<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function Person(name) {
            this.name = name;
            this.getName = function () {
                console.log(this);
                return this.name
            }
        }

        const jack = new Person('jack')
        console.log(jack.getName()); //谁调用就指向谁 jack
        const { getName } = jack
        console.log(getName()); //直接调用函数，指向window
        //普通函数直接调用指向window
        function myFun(params) {
            console.log(this);
        }

        myFun()

        const o = { name: 'tom', func: myFun }

        console.log(o.func());  //谁调用就指向谁

        const obj = {
            message: 'hello',
            // logMsg:function(){} es6函数简写
            logMsg() {
                const message = 'abc';
                return this.message
            }
        }

        console.log(obj.logMsg()); //hello

        setTimeout(() => {
            console.log(this); //window
            console.log(obj.logMsg());  //hello
        }, 1000);

        var length =4;
        function callBack(params) {
            console.log(this);
            console.log(this.length); 
        }
console.log(callBack());
        const myObj = {
            length:5,
            method(){
                arguments[0]()
            }
        }
        myObj.method(callBack,1,2,3,4) //5 arguments可以拿到实时传进来的实参，所以能拿到length


        var myName = 'the window';
        let object = {
            myName: 'object',
            getNameFunc(){
                function helper() {
                    console.log(this);
                    return this.myName
                }
              return  helper()  //单独调用，没有前缀调用，所以指向window
            }
        }

        console.log(object.getNameFunc());
    </script>
</body>

</html>